/**
 * 
 */
package appControll;

import java.util.*;

import exceptions.*;

import business.*;

import model.*;

/**
 * @author Anthares & Hennek
 *
 */
public class AppController {
	private static AppController singleton;
	private SectionManager sectMan;
	private LoginManager loginMan;
	private EtudiantManager etuMan;
	private SyllabusManager sylMan;
	private CommandeEtuManager comEtuMan;
	private LotManager lotMan;
	private RechercheManager rechMan;
	private ConnectionSettingsManager connSetMan;
	private AppSettingsManager appSetMan;
	private OrderStatusManager ordStatMan;
	private ProfesseurManager profMan;
	
	private AppController(){}
	public static AppController getUniqueInstance(){
		if(singleton == null) singleton = new AppController();
		return singleton;
	}
	
	public ArrayList<Section> getAllSection() throws PasIdentificationException, DatabaseErrorException {
		if(sectMan == null) sectMan = new SectionManager();
		return sectMan.getAllSection();
	}
	public MembreComite identification(String userLogin, String userPass, ConnectionSettings connset) throws ErreurIdentification, PasIdentificationException, BadLoginException, ODBCErrorException, BadPasswordException, DatabaseErrorException, InvalidDataException{
		if(loginMan == null) loginMan = new LoginManager();
		return loginMan.identification(userLogin, userPass, connset);
	}
	public ArrayList<Etudiant> getEtudiant(int annee, int section_id) throws PasIdentificationException, DatabaseErrorException, InvalidDataException{
		if(etuMan == null) etuMan = new EtudiantManager();
		return etuMan.getEtudiant(annee, section_id);
	}
	/**
	 * @param annee
	 * @param section_id
	 * @return
	 * @throws PasIdentificationException 
	 * @throws DatabaseErrorException 
	 * @throws InvalidDataException 
	 */
	public ArrayList<Syllabus> getSyllabus(int annee, int section_id) throws PasIdentificationException, DatabaseErrorException, InvalidDataException {
		if(sylMan==null) sylMan = new SyllabusManager();
		return sylMan.getSyllabus(annee,section_id);
	}
	public double getPrixCommande(Object object) throws PasIdentificationException, DatabaseErrorException, InvalidDataException{
		if(comEtuMan==null)comEtuMan=new CommandeEtuManager();
		return comEtuMan.getPrixCommande(object);
	}
	public void addCommandeEtu(Commande_etu com) throws PasIdentificationException, DatabaseErrorException, InvalidDataException{
		if(comEtuMan==null)comEtuMan=new CommandeEtuManager();
		comEtuMan.addCommandeEtu(com);
	}
	public Lot getLastLot() throws PasIdentificationException, DatabaseErrorException{
		if(lotMan==null) lotMan = new LotManager();
		return lotMan.getLastLot();
	}
	public Object getAllCommande(int type, int annee, int section_id, OrderStatus ordStatus) throws PasIdentificationException, DatabaseErrorException, InvalidDataException{
		if(comEtuMan==null)comEtuMan = new CommandeEtuManager();
		switch(type){
		case 0: return comEtuMan.getAllCommandeEtu(annee, section_id, ordStatus);
		default: return null;
		}
	}
	public void modifCommandeEtu(Commande_etu com) throws PasIdentificationException, DatabaseErrorException, InvalidDataException{
		if(comEtuMan==null)comEtuMan = new CommandeEtuManager();
		comEtuMan.modifCommandeEtu(com);
	}
	public void delCommandeEtu(Commande_etu com) throws PasIdentificationException, DatabaseErrorException, InvalidDataException{
		if(comEtuMan==null)comEtuMan = new CommandeEtuManager();
		comEtuMan.delCommandeEtu(com);
	}
	public ArrayList<Etudiant> rechercheEtudiant(int syllabus_id, Boolean paye, String etat) throws PasIdentificationException, DatabaseErrorException, InvalidDataException{
		if(rechMan==null)rechMan=new RechercheManager();
		return rechMan.rechercheEtudiant(syllabus_id, paye, etat);
	}
	public ArrayList<Commande_etu> rechercheSyllabusEtudiant(String etu) throws PasIdentificationException, DatabaseErrorException, InvalidDataException{
		if(rechMan==null)rechMan=new RechercheManager();
		return rechMan.rechercheSyllabusEtudiant(etu);
	}
	public ArrayList<Syllabus> rechercheSyllabusDispo(Section sect, int annee) throws PasIdentificationException, DatabaseErrorException, InvalidDataException{
		if(rechMan==null)rechMan=new RechercheManager();
		return rechMan.rechercheSyllabusDispo(sect, annee);
	}
	public int getNbrComEtu() throws PasIdentificationException, DatabaseErrorException{
		if(comEtuMan==null)comEtuMan = new CommandeEtuManager();
		return comEtuMan.getNbrComEtu();
	}
	public void saveConnectionSettings(ConnectionSettings connset) {
		if(connSetMan==null)connSetMan=new ConnectionSettingsManager();
		connSetMan.saveConnectionSettings(connset);
	}
	public ConnectionSettings loadConnectionSettings(){
		if(connSetMan==null)connSetMan=new ConnectionSettingsManager();
		return connSetMan.loadConnectionSettings();
	}
	public HashMap<Syllabus, Integer> getSyllabusCommandeEtu(String status) throws PasIdentificationException, DatabaseErrorException{
		if(sylMan==null)sylMan=new SyllabusManager();
		return sylMan.getSyllabusCommandeEtu(status);
	}
	public int addEtudiant(Etudiant etu) throws PasIdentificationException, DatabaseErrorException, EmptyFormException{
		if(etuMan==null)etuMan=new EtudiantManager();
		return etuMan.addEtudiant(etu);
	}
	public void modifEtudiant(Etudiant etu) throws PasIdentificationException, DatabaseErrorException, EmptyFormException{
		if(etuMan==null)etuMan=new EtudiantManager();
		etuMan.modifEtudiant(etu);
	}
	public HashMap<AppSettingsEnum, AppSettings> loadAppSettings() throws DatabaseErrorException, PasIdentificationException{
		if(appSetMan==null) appSetMan=new AppSettingsManager();
		return appSetMan.loadAppSettings();
	}
	public void saveAppSettings(HashMap<AppSettingsEnum, AppSettings> appSet) throws DatabaseErrorException, PasIdentificationException, InvalidSettingException{
		if(appSetMan==null) appSetMan=new AppSettingsManager();
		appSetMan.saveAppSettings(appSet);
	}
	public ArrayList<OrderStatus> getAllOrderStatus() throws PasIdentificationException, DatabaseErrorException {
		if(ordStatMan==null)ordStatMan= new OrderStatusManager();
		return ordStatMan.getAllOrderStatus();
	}
	/**
	 * @param i
	 * @return
	 * @throws DatabaseErrorException 
	 * @throws PasIdentificationException 
	 */
	public OrderStatus getOrderStatus(int i) throws PasIdentificationException, DatabaseErrorException {
		if(ordStatMan==null)ordStatMan= new OrderStatusManager();
		return ordStatMan.getOrderStatus(i);
	}
	public void logout() throws DatabaseErrorException {
		if(loginMan==null)loginMan=new LoginManager();
		loginMan.logout();
	}
	public ArrayList<Professeur> getAllProf() throws PasIdentificationException, DatabaseErrorException {
		if(profMan==null) profMan = new ProfesseurManager();
		return profMan.getAllProf();
	}
	public int addSyllabus(Syllabus syl) throws EmptyFormException, PasIdentificationException, DatabaseErrorException {
		if(sylMan==null) sylMan = new SyllabusManager();
		return sylMan.addSyllabus(syl);
	}
}
